#pragma once
#include "stdafx.h"
#include "Effect.h"
#include "PreHeader.h"
#include "Texture.h"
#include "Mesh.h"
/*
	This is a basic class of all the objects which could been rendered.
	When create the resource? It couldn't create in constroctor because
	the component haven't added to manager, Can't send message.
	So init the render object when add to render queue.
	Because add a render object to render queue means this object could be
	render now. This allow the render object create resource latter.
	(not add to render queue)
*/

class Component;
class Effect;
class Texture;

class RenderObject : public Component
{
private:
	RenderObject();
	
public:

	struct VertexTexStr
	{
		XMFLOAT3 pos;
		XMFLOAT2 tex;
	};
	
	struct VertexColorStr
	{
		XMFLOAT3 pos;
		XMCOLOR color;
	};

	struct RenderObjectStr
	{
		RenderObjectStr() {}
		~RenderObjectStr()
		{
			if(verticesBuffer!=NULL)
			{
				verticesBuffer->Release();
				verticesBuffer=NULL;
			}
			
			if(indexsBuffer!=NULL)
			{
				indexsBuffer->Release();
				indexsBuffer=NULL;
			}
			
			if(samplerState!=NULL)
			{
				samplerState->Release();
				samplerState=NULL;
			}
		}
		ID3D11Buffer* verticesBuffer;
		ID3D11Buffer* indexsBuffer;
		ID3D11SamplerState* samplerState;
		Texture::sPtr texture;
		Vertices* vertices;
		Texs* texs;
		unsigned int numIndexs;
	};

	/*
		Render mode should descript camera, texture, render operator in this render object
		1.RenderWithCameraWithTexture: has camera, has texture, use D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST 
		2.RenderWithCameraNoTexture: has camera, no texture use D3D11_PRIMITIVE_TOPOLOGY_LINELIST
		
	*/
	
	enum RenderMode
	{
		RenderWithCameraWithTexture,
		RenderWithCameraNoTexture
	};
	
	typedef Vector<RenderObjectStr*> RenderObjs;
	typedef Vector<RenderObjectStr*>::iterator RenderObjsIter;
	typedef boost::shared_ptr<RenderObject> sPtr;

	RenderObject(CompName,string,Mesh::sPtr,RenderMode);
	virtual ~RenderObject();
	virtual void AddedToObject();
	virtual ID3D11Buffer* CreateVertexBuffer(Vertices*,Texs*);
	virtual ID3D11Buffer* CreateIndexBuffer(Indexs*);
	virtual ID3D11SamplerState* CreateSamplerState();
	void UpdatePreFrame(const int);
	virtual void Render();
	virtual void Initial(const boost::shared_ptr<Effect>&);
	virtual XMMATRIX GetWorldMatrix() = 0;
	virtual void CalcAnim(const int) =0;
	string GetName();

private:
	string m_name;
	RenderMode m_mode;
	ID3D11InputLayout* m_vertexLayout;
	ID3D11SamplerState* m_defaultSampler;
	RenderObjs m_renderObjs;
	Mesh::sPtr m_mesh;
	Effect::sPtr m_effect;
};